package com.leyou.item.controller;

import com.leyou.common.pojo.PageResult;
import com.leyou.item.pojo.Brand;
import com.leyou.item.pojo.Category;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * http://api.leyou.com/api/item/brand/page?key=&page=1&rows=5&sortBy=id&desc=false
     *根据条件查询分页并排序查询品牌信息
     * @param key
     * @param page
     * @param rows
     * @param sortBy
     * @param desc
     * @return
     */
    @GetMapping("page")
    @ResponseBody
    public ResponseEntity<PageResult<Brand>> queryBrandsByPage(
            @RequestParam(value = "key",required = false) String key,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "sortBy",required = false) String sortBy,
            @RequestParam(value = "desc", required = false) Boolean desc
    ){
        PageResult<Brand> result = this.brandService.queryBrandsByPage(key, page, rows, sortBy, desc);

        if (CollectionUtils.isEmpty(result.getItems())){

            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 新增品牌
     * @param brand
     * @param cids
     * @return
     */

    @PostMapping()
    public ResponseEntity saveBrands(Brand brand,
                                       @RequestParam("cids") List<Long> cids ){

        this.brandService.saveBrands(brand,cids);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改品牌
     * @param brand
     * @return
     */
    @PutMapping()
    public ResponseEntity updateBrands(Brand brand,
                                       @RequestParam("cids") List<Long> cids){

        this.brandService.updateBrands(brand,cids);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 删除品牌和中间表
     * @param bid
     * @return
     */
    @GetMapping("delete/bid/{bid}")
    public ResponseEntity deleteBrands( @PathVariable("bid")Long bid){

        this.brandService.deleteBrands(bid);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //api.leyou.com/api/item/brand/cid/698

    /**
     * 根据cid查询品牌
     * @param cid
     * @return
     */
    @GetMapping("cid/{cid}")
    public ResponseEntity<List<Brand>> queryBrandByCid(@PathVariable Long cid){
        List<Brand> brandList = this.brandService.queryBrandByCid(cid);

        if(brandList == null || CollectionUtils.isEmpty(brandList)){
            return ResponseEntity.notFound().build();
        }

        return ResponseEntity.ok(brandList);
    }

    /**
     * 根据品牌id查询品牌
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<Brand> queryBrandById(@PathVariable("id") Long id){
        Brand brand = this.brandService.queryBrandById(id);
        if (brand == null){
            return ResponseEntity.notFound().build();
        }

        return ResponseEntity.ok(brand);
    }
















}
